package cn.edu.gzhmt.order.admin.web.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.gzhmt.order.admin.service.IAdminOrderService;
import cn.edu.gzhmt.order.admin.service.impl.AdminOrderServiceImpl;
import cn.edu.gzhmt.order.domain.Order;
import cn.edu.gzhmt.utils.commons.CommonUtils;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

/**
 * 后台订单管理模块：web层
 * @author azhuo
 *
 */
public class AdminOrderServlet extends BaseServlet {

	private IAdminOrderService adminOrderService = new AdminOrderServiceImpl();

	/**
	 * 查询所有订单
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Order> orderList = adminOrderService.findAllOrder();

		request.setAttribute("orderList", orderList);

		return "/adminjsps/order/orderList.jsp";
	}

	/**
	 * 发货
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String sendGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String oid = request.getParameter("oid");

		adminOrderService.sendGoods(oid);

		return this.findAllOrder(request, response);
	}

	/**
	 * 查找订单
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Order form = CommonUtils.toBean(request.getParameterMap(), Order.class);

		List<Order> orderList = adminOrderService.findOrder(form);
		if (orderList == null || orderList.size() <= 0) {
			return this.findAllOrder(request, response);
		}

		request.setAttribute("orderList", orderList);

		return "f:/adminjsps/order/orderList.jsp";
	}

	/**
	 * 根据oid删除订单
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String oid = request.getParameter("oid");

		adminOrderService.removeOrder(oid);

		response.getWriter().print(oid);

		return null;
	}

	/**
	 * 加载订单信息：条目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadOrderItem(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String oid = request.getParameter("oid");

		// 根据订单号查询条目信息
		Order order = adminOrderService.loadOrderItem(oid);
		request.setAttribute("order", order);

		return "f:/adminjsps/order/orderItemList.jsp";
	}

	/**
	 * 订单统计
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String orderStatistics(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String rd_time = request.getParameter("rd_time");
		if (rd_time == null) {
			rd_time = "year";
		}
		System.out.println("rd_time = " + rd_time);

		if ("year".equals(rd_time)) {
			// SimpleDateFormat dataFormat = new SimpleDateFormat("YYYY-mm-DD");
			Calendar cale = Calendar.getInstance();

			int year = cale.get(Calendar.YEAR);
			int month = cale.get(Calendar.MONTH) + 1;
			System.out.println("year = " + year);
			System.out.println("month = " + month);
		}

		// // 根据订单号查询条目信息
		// Order order = adminOrderService.loadOrderItem(oid);
		// request.setAttribute("order", order);

		return "f:/adminjsps/statistics/turnover.jsp";
	}

	/**
	 * 订单统计查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String statisticsOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String date = request.getParameter("date");

		if (date == null || date.trim().length() < 1) {
			date = "all";
		}

		// 按照特定时间查询订单
		List<Order> orderList = adminOrderService.statisticsOrder(date);
		request.setAttribute("orderList", orderList);

		return "/adminjsps/statistics/turnover.jsp";
	}

}
